#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#include "../../liboctopus/src/octopus.h"


void *threadDevice( void *ptr );

pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;

main()
{
  pthread_t thread1, thread2;
     
  int  iret1, iret2;


  struct octopus_context octopus1;
  struct octopus_context octopus2;
  
  if(!octopus_init(&octopus1)){
    printf("%s\n",&octopus1.error_str);
    exit(0);
  }
  
  if(octopus_open_serial(&octopus1,"20081108")<0){
    printf("%s\n",octopus1.error_str);
    exit(0);
  }
  
  if(!octopus_init(&octopus2)){
    printf("%s\n",&octopus2.error_str);
    exit(0);
  }
  
  if(octopus_open_serial(&octopus2,"20081109")<0){
    printf("%s\n",octopus2.error_str);
    exit(0);
  }

  /* Create independent threads each of which will execute function */
  iret1 = pthread_create( &thread1, NULL, threadDevice, (void*)&octopus1);
  iret2 = pthread_create( &thread2, NULL, threadDevice, (void*)&octopus2);

  /* Wait till threads are complete before main continues. Unless we  */
  /* wait we run the risk of executing an exit which will terminate   */
  /* the process and all threads before the threads have completed.   */

  pthread_join( thread1, NULL);
  pthread_join( thread2, NULL); 

  printf("Thread 1 returns: %d\n",iret1);
  printf("Thread 2 returns: %d\n",iret2);
  exit(0);
}

void *threadDevice( void *ptr )
{
  struct octopus_context * octopus;
  octopus = (struct octopus_context *)ptr;

  char desc[64];

  while(1){
    octopus_get_hwdesc(octopus, desc);
  }
}
